using System.Reflection;
using NUnit.Framework;
using ContentRenderingApi;
using ContentRenderingApiSystemTest.Helper;
using ContentRenderingApi.Path;

namespace ContentRenderingApiSystemTest.TestFixture
{
    [TestFixture]
    public class RenderOverrideTestFixture
    {
        [Test]
        public void PreTest()
        {
            AbstractTemplate template = new EmbeddedResourceTemplate(
                Assembly.GetExecutingAssembly(),
                "ContentRenderingApiSystemTest.TestFixture.Input",
                "PreOverride");

            template.AddNamedParameter("testone", "zzz");
            template.AddNamedParameter("testone", "aaa");
            template.AddNamedParameter("testone", "sss");
            template.AddNamedParameter("testtwo", "sss");

            template.TransformationConfiguration.AddPreOverride(
                new PartialPath("testtwo"),
                (nodeExtender) => new object[] { "111", "222" });

            template.TransformationConfiguration.AddPreOverride(
                new PartialPath("testthree"),
                (nodeExtender) => new object[] { "010", "020" });

            Assert.AreEqual(
                ResourceHelper.FetchString("ContentRenderingApiSystemTest.TestFixture.Output.PreOverride.txt"),
                template.Render());
        }

        [Test]
        public void PostTest()
        {
            AbstractTemplate template = new EmbeddedResourceTemplate(
                Assembly.GetExecutingAssembly(),
                "ContentRenderingApiSystemTest.TestFixture.Input",
                "PostOverride");

            template.AddNamedParameter("testone", "zzz");
            template.AddNamedParameter("testone", "aaa");
            template.AddNamedParameter("testone", "sss");

            template.TransformationConfiguration.AddPostOverride(
                new PartialPath("testone"),
                (nodeExtender, values) =>
                {
                    for (int index = 0; index < values.Count; index++)
                    {
                        values[index] = "*" + ((string)values[index]) + "*";
                    }

                    // add a new entry
                    values.Add("111");
                });

            Assert.AreEqual(
                ResourceHelper.FetchString("ContentRenderingApiSystemTest.TestFixture.Output.PostOverride.txt"),
                template.Render());
        }

        [Test]
        public void WildcardTest()
        {
            AbstractTemplate template = new EmbeddedResourceTemplate(
                Assembly.GetExecutingAssembly(),
                "ContentRenderingApiSystemTest.TestFixture.Input",
                "EmptyOverride");

            template.AddNamedParameter("testone", "test");
            template.AddNamedParameter("testtwo", "test");

            template.TransformationConfiguration.AddWildcardOverride(
                new PartialPath("testone"),
                (nodeExtender, name, values) =>
                {
                    if (name == "testa")
                    {
                        values.Add("valuea");
                    }

                    if (name == "testc")
                    {
                        values.Add("valuec");
                    }
                });

            Assert.AreEqual(
                ResourceHelper.FetchString("ContentRenderingApiSystemTest.TestFixture.Output.EmptyOverride.txt"),
                template.Render());
        }
    }
}
